<style>
body {
  min-width:357px;
  overflow-x:hidden;
}

img {
  margin:5px;
  border:2px solid black;
  vertical-align:middle;
  width:75px;
  height:75px;
}
</style>


<script>

 function CrossSocial()
 {
  alert("CrossSocial");
 }

 function CreateFaceonlineWindow()
 {
  chrome.windows.create(
    {
      url: "http://comet2.lovesupport.ru/html/faceonline/faceonline.html",
      /*tabId:1,*/
      focused:true,
      height:520,
      width: 512,
      type:"popup"
    }
    ,
    function(win)
    {
      window.faceonlineWindowId = win.id;
      window.faceonlineTabId = win.tabs[0].id;
      // window.faceonlinePort = chrome.tabs.connect(window.faceonlineTabId, { name:"faceonlinePort" } );
    }
  )
 }

 chrome.browserAction.onClicked.addListener(
  function(tab) {
    try{
      if ( window.faceonlineWindowId === undefined || window.faceonlineTabId === undefined )
        CreateFaceonlineWindow();
      else
      {
        chrome.tabs.get(window.faceonlineTabId, function(tab) {
          if ( tab === undefined )
             CreateFaceonlineWindow();
          else
          {
            chrome.windows.update(window.faceonlineWindowId, { focused:true }, function(win) {
              if ( win === undefined )
                CreateFaceonlineWindow();
            }); // update
          }
        }); // chrome.tabs.get
      }
    }
    catch(e)
    {
      alert(e.message);
    }
 });
 
 
function FireSiteList()
{
  if ( !window.faceonlinePort )
    return;
  
  chrome.tabs.query({}, function(tabs){
    var siteList=[];
    
    for( var i=0; i < tabs.length; ++i)
      siteList.push(tabs[i].url);

    if ( window.faceonlinePort )
      window.faceonlinePort.postMessage({message: "eventSiteList", values: siteList});
    
  });
  /*chrome.tabs.getAll({populate:true}, function(wins){
    var siteList=[];
    for( var i=0; i < wins.lenght; ++i)
    {
      //siteList.push(wins[i].url);
      wins[i].tabs
    }
    
    if ( window.faceonlinePort )
    {
      window.faceonlinePort.postMessage({message: "eventSiteList", values: siteList});
    }
  });*/
}

 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { 
   FireSiteList();
});
 
setInterval(FireSiteList, 60000);

chrome.extension.onConnect.addListener(function(port) {
  if (port.name == "faceonlinePort")
  {
    window.faceonlinePort = port;
    port.onDisconnect.addListener(function() {
      window.faceonlinePort = null;
    });
    FireSiteList();
  }
});


/*
var req = new XMLHttpRequest();
req.open(
    "GET",
    "http://api.flickr.com/services/rest/?" +
        "method=flickr.photos.search&" +
        "api_key=90485e931f687a9b9c2a66bf58a3861a&" +
        "text=hello%20world&" +
        "safe_search=1&" +  // 1 is "safe"
        "content_type=1&" +  // 1 is "photos only"
        "sort=relevance&" +  // another good one is "interestingness-desc"
        "per_page=20",
    true);
req.onload = showPhotos;
req.send(null);

function showPhotos() {
  var photos = req.responseXML.getElementsByTagName("photo");

  for (var i = 0, photo; photo = photos[i]; i++) {
    var img = document.createElement("image");
    img.src = constructImageURL(photo);
    document.body.appendChild(img);
  }
}

// See: http://www.flickr.com/services/api/misc.urls.html
function constructImageURL(photo) {
  return "http://farm" + photo.getAttribute("farm") +
      ".static.flickr.com/" + photo.getAttribute("server") +
      "/" + photo.getAttribute("id") +
      "_" + photo.getAttribute("secret") +
      "_s.jpg";
}
*/

</script>
